<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
use api\wxapp\model\Staff;
use api\wxapp\model\User;
class StaffController extends AdminBaseController
{
    public function index()
    {
        $params = $this->param();
        $where = array();
        if(isset($params['nickname'])){
            $where[] = ['t2.nickname','like','%'.$params['nickname'].'%'];
        }
        $list = Staff::alias('t1')
            ->join('app_user t2','t2.id = t1.uid')
            ->field('t1.id,t1.uid,t1.depart,t1.addtime,t2.nickname')
            ->where($where)
            ->paginate(5);
        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    public function add()
    {
        if($_POST){
            $validate = new Validate([
                'uid'  => 'require',
                'depart' => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            if(!User::get($_POST['uid'])){
                $this->error('用户不存在!!!!');
            }
            if(Staff::where('uid',$_POST['uid'])->find()){
                $this->error('当前用户已是员工!!!!');
            }
            $_POST['addtime'] = date('Y-m-d H:i:s',time());
            $res = Staff::insert($_POST);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }
    public function edit()
    {
        $params = $this->param();
        if($_POST){
            $validate = new Validate([
                'depart'  => 'require',
            ]);
            $res = Staff::update($_POST);
            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }

        }else{
            $info = Staff::get($params['id']);
            $info['nickname'] = User::where('id',$info['uid'])->value('nickname');
            $this->assign('info',$info);
            return $this->fetch();
        }
    }
    public function delete()
    {
        $params = $this->param();
        $res = Staff::destroy($params['id']);
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}
